<?php
/**
 * controller file Created by PhpStorm.
 * User: wumengmeng
 * Date: 2019/10/26
 * Time: 09:59
 */

namespace Modules\Shop\Http\Controllers;

use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Request;
use Modules\Shop\Service\StoreOrderService;
use Excel;

class StoreOrderController extends BaseController
{
    /**
     *  普通商品订单 列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function index()
    {
        $arr_search = (array)Request::all();
        $arr_search['shop_id'] = shop_shop_id();//门店id
        $res = StoreOrderService::get_list($arr_search);
        echo_error($res);

        return view('shop::store_order.index', $res['data']);
    }

    /**
     * 查看订单详情
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function detail()
    {
        $nId = Request::get('id', 0);
        $res = StoreOrderService::get_one((int)$nId);
        echo_error($res);

        $arr_data = [
            'info' => $res['data'],
        ];
        return view('shop::store_order.detail', $arr_data);
    }

    /**
     * 删除
     *
     * @return array|\Illuminate\Http\RedirectResponse
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function delete()
    {
        $n_id = (int)Request::get('id', 0);
        return StoreOrderService::delete_one($n_id);
    }

    /**
     * 快速发货
     * 2019 11 09
     */
    public function sendGoods()
    {
        $data = (array)Request::all();

        shiwu_start();
        $res = StoreOrderService::sendGoods($data);
        shiwu_end($res);

        return $res;
    }

    /**
     * 更改订单状态
     * 2019 11 09
     */
    public function changStatus()
    {
        $data = (array)Request::all();

        shiwu_start();
        $res = StoreOrderService::changStatus($data);
        shiwu_end($res);

        return $res;
    }

    /**
     * 导出Excel
     */
    public function export()
    {
        $arr_search = (array)Request::all();
        $arr_search['shop_id'] = shop_shop_id();//门店id
        $list = StoreOrderService::get_order_list($arr_search);
        $orderList = [];
        foreach ($list as $key => $value) {
            $orderList[$value['order_status']][] = $value;
        }

        $cellLetter = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $title = "订单列表" . date('Y-m-d h:i');
        Excel::create($title, function ($excel) use ($orderList, $cellLetter) {
            foreach ($orderList as $sheet_name => $item) {
                $cellData = [];
                $cellData[0] = ['订单编号', '订单状态', '收货人', '联系方式', '收货地址', '商品名称【规格】', '价格', '数量', '订单总金额', '运费', '优惠金额', '实付金额', '支付方式', '付款时间', '下单时间', '快递公司', '快递单号', '发货时间', '收货时间', '买家留言'];
                $num = [];
                foreach ($item as $key => $value) {
                    $user_name = $value['get_address']['username'];
                    $user_phone = $value['get_address']['phone'];
                    $user_address = $value['get_address']['province'] . $value['get_address']['city'] . $value['get_address']['area'] . $value['get_address']['address'];

                    if ($value['pay_type'] == 1) {
                        $pay_type = '微信';
                    } elseif ($value['pay_type'] == 2) {
                        $pay_type = '支付宝';
                    } else {
                        $pay_type = '--';
                    }
                    // $pay_type = $value['pay_type'] == 1 ? '微信' : '支付宝';
                    $express = $value['get_express']['name'];

                    if (!empty($value['get_order_goods'])) {
                        foreach ($value['get_order_goods'] as $ky => $item) {
                            $goods = $item['goods_product']['goods_info']['name'] . '【' . $item['goods_product']['specs']['name'] . '】';
                            $goods_price = $item['goods_price'];
                            $goods_num = $item['goods_num'];
                            $cellData[] = [
                                $value['order_no'],       //订单编号
                                $value['order_status'],   //订单状态
                                $user_name, $user_phone, $user_address, $goods, $goods_price, $goods_num,
                                $value['order_amount'],   //订单总金额
                                $value['delivery_price'], //运费
                                $value['coupon_price'],   //优惠金额
                                $value['pay_amount'],     //实付金额
                                $pay_type,                //支付方式
                                $value['pay_time'],       //付款时间
                                $value['created_at'],     //下单时间
                                $express,                 //快递公司
                                $value['delivery_no'],    //快递单号
                                $value['delivery_time'],  //发货时间
                                $value['receive_time'],   //收货时间
                                $value['remark'],         //买家留言
                            ];

                        }
                        $num[$key] = count($value['get_order_goods']);
                    }
                }
                $excel->sheet($sheet_name, function ($sheet) use ($cellData, $num, $cellLetter) {
                    $sheet->rows($cellData);
                    $sheet->getDefaultRowDimension()->setRowHeight(22);//设置行高
                    $sheet->getStyle('A1:T' . count($cellData))->getAlignment()->setVertical('center');//垂直居中
                    for ($i = 0; $i <= count($cellData[0]); $i++) {
                        $cell = $cellLetter[$i];
                        if (in_array($cell, ['A', 'E'])) {
                            $width = 25;
                        } elseif (in_array($cell, ['B', 'C', 'D', 'G', 'H', 'I', 'J', 'K', 'L', 'M'])) {
                            $width = 15;
                        } elseif (in_array($cell, ['F'])) {
                            $width = 50;
                        } else {
                            $width = 20;
                        }
                        $sheet->setWidth($cell, $width);//设置列的宽度

                        if ($cell != 'F' & $cell != 'G' & $cell != 'H') {
                            $start = 2;
                            foreach ($num as $key => $value) {
                                $end = $start + $value - 1;
                                $sheet->mergeCells($cell . $start . ':' . $cell . $end); //合并单元格
                                $start = $end + 1;
                            }
                        }
                    }
                    $sheet->getStyle('A1:T' . count($cellData))->applyFromArray([
                        'alignment' => ['horizontal' => 'center'],//设置居中
                    ]);
                    $sheet->getStyle('E2:F' . count($cellData))->applyFromArray([
                        'alignment' => ['horizontal' => 'left'],
                        'font' => ['size' => 10]//字体设置
                    ]);
                    $sheet->getStyle('A1:T1')->applyFromArray([
                        'font' => ['bold' => true, 'color' => ['rgb' => '0072ff']]//字体设置
                    ]);
                });
            }

            $excel->setactivesheetindex(0);//设置默认打开第一个sheet

        })->export('xls');

    }


}